//
//  PresentationController.swift
//  swift-微博
//
//  Created by chenrin on 2016/12/15.
//  Copyright © 2016年 zhuoheng. All rights reserved.
//

import UIKit

class PresentationController: UIPresentationController {
    
    
    var presentFrame = CGRect.zero
    
    
    fileprivate lazy var coverView : UIView = {
       let view = UIView()
        view.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        view.frame = UIScreen.main.bounds
        
        //添加监听
        let tap = UITapGestureRecognizer(target: self, action: #selector(close))
        view.addGestureRecognizer(tap)
        
        return view
    }()
    
   override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
    }
    
    //即将布局转场子视图时调用
   override func containerViewWillLayoutSubviews(){
         //1.修改弹出视图的大小
    if presentFrame == CGRect.zero {
        presentedView?.frame = CGRect(x: 100, y: 56, width: 200, height: 200)

    }else{
        presentedView?.frame = presentFrame
    }
          //2.在容器视图上添加一个蒙版，插入到展现视图的下面
           //containerView : 容器
        containerView?.insertSubview(coverView, at: 0)
    
    }
    
    func close() {
        presentingViewController.dismiss(animated: true, completion: nil)
    }
}
